﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WPF_SFrost
{
    public partial class FormContactList : Form
    {
        public SqlConnection conn = new SqlConnection(SqlDbHelper.ConnectionString);
        protected SqlCommand cmd;
        public SqlDataAdapter sda;
        public DataSet ds;
        public FormContactList()
        {
            InitializeComponent();
        }

        private void FormContactList_Load(object sender, EventArgs e)
        {
            Fill();
        }

        public void Fill()
        {
            try
            {
                conn.Open();
                cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                if (cboCondition.Text == "姓名")
                {
                    cmd.CommandText = "GetContactListByName";   //前面的命令类型改为StoredProcedure后，这儿不用再加 Execute命令了
                    cmd.Parameters.AddWithValue("@name", txtSearch.Text.Trim());
                }
                else
                {
                    cmd.CommandText = "GetContactListByPhone";
                    cmd.Parameters.AddWithValue("@phone", txtSearch.Text.Trim());
                }
                sda = new SqlDataAdapter(cmd);
                ds = new DataSet();
                sda.Fill(ds);
                dgvContactList.DataSource = ds.Tables[0];
                conn.Close();
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dgvContactList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void FormContactList_Resize(object sender, EventArgs e)
        {
            

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            Fill();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FormContactAdd f = new FormContactAdd();
            f.ShowDialog();
            Fill();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int id = 0;
            try
            {
                id = (int)dgvContactList.CurrentRow.Cells[0].Value;
            }catch(Exception ex)
            {
                MessageBox.Show("请选择有效的记录！");
                return;
            }
            DialogResult dr = MessageBox.Show("是否确定删除选择的联系人？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                cmd = new SqlCommand("deletecontactbyid", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@id", id);
                conn.Open();
                int n = cmd.ExecuteNonQuery();

                //string sql = "deletecontactbyid";
                //SqlParameter[] parameters =
                //{
                //    new SqlParameter("@Id",SqlDbType.Int)
                //};
                //parameters[0].Value = id;
                //int n = SqlDbHelper.ExecuteNonQuery(sql, CommandType.StoredProcedure, parameters);

                if (n == 1)
                {
                    MessageBox.Show("成功删除联系人！");
                }
                else
                {
                    MessageBox.Show("删除联系人失败！");
                }
            }
            conn.Close();
            Fill();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvContactList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int id = 0;
            try
            {
                id = (int)dgvContactList.CurrentRow.Cells[0].Value;
            }catch(Exception ex)
            {
                MessageBox.Show("请选择有效的记录！");
                return;
            }
            FormContactDetail f = new FormContactDetail(id);
            f.ShowDialog();
            Fill();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int id = 0;
            try
            {
                id = (int)dgvContactList.CurrentRow.Cells[0].Value;
            }
            catch (Exception ex)
            {
                MessageBox.Show("请选择有效的记录！");
                return;
            }
            FormContactDetail f = new FormContactDetail(id);
            f.ShowDialog();
            Fill();
        }
    }
}
